// pages/personnel/personnel.js
var apiHelp = require('../../utils/api.js');
var util = require('../../utils/util.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    person:{},
    customerTypeFlag:false,
    cardTypeFlag: false,
    delFlag:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (!options.contactId){
      wx.setNavigationBarTitle({ title: '添加乘车人' })
    }else{
      wx.setNavigationBarTitle({ title: '修改乘车人' })
      var params = { "id": options.contactId };
      var url = apiHelp.getContactById();

      apiHelp.request(url, 'GET', params, function (data, err) {
        that.setData({
          person: data,
          name: data.name,
          customerType: data.customerType,
          cardType: data.cardType,
          cardNo: data.cardNo,
          telephone: data.telephone,
          id: data.id,
          customerTypeFlag: true,
          cardTypeFlag: true,
          delFlag:false
        });
      });
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
 /**在线选座弹框
   * 
   */
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  }, showModal1: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation1 = animation
    animation.translateY(300).step()
    this.setData({
      animationData1: animation.export(),
      showModalStatus1: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData1: animation.export()
      })
    }.bind(this), 200)
  }, hideModal1: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation1 = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData1: animation.export(),
        showModalStatus1: false
      })
    }.bind(this), 200)
  }, pouponAdult: function (e) {
    var customertype = e.currentTarget.dataset.customertype;
    this.setData({
      bgpositionAdult: '0 -225rpx',
      bgpositionChild: '-55rpx -226rpx',
      bgpositionStudent:'-55rpx -226rpx',
      bgpositionDisability: '-55rpx -226rpx',
      customerType: customertype,
      customerTypeFlag:true
    });
  }, pouponChild: function (e) {
    var customertype = e.currentTarget.dataset.customertype;
    this.setData({
      bgpositionAdult: '-55rpx -226rpx',
      bgpositionChild: '0 -225rpx',
      bgpositionStudent: '-55rpx -226rpx',
      bgpositionDisability: '-55rpx -226rpx',
      customerType: customertype,
      customerTypeFlag: true
    });
  }, 
  pouponStudent: function (e) {
    var customertype = e.currentTarget.dataset.customertype;
    this.setData({
      bgpositionAdult: '-55rpx -226rpx',
      bgpositionChild: '-55rpx -226rpx',
      bgpositionStudent: '0 -225rpx',
      bgpositionDisability: '-55rpx -226rpx',
      customerType: customertype,
      customerTypeFlag: true
    });
  },
  pouponDisability: function (e) {
    var customertype = e.currentTarget.dataset.customertype;
    this.setData({
      bgpositionAdult: '-55rpx -226rpx',
      bgpositionChild: '-55rpx -226rpx',
      bgpositionStudent: '-55rpx -226rpx',
      bgpositionDisability: '0 -225rpx',
      customerType: customertype,
      customerTypeFlag: true
    });
  },
  pouponID: function (e) {
    var cardType = e.currentTarget.dataset.cardtype;
    this.setData({
      bgpositionID: '0 -225rpx',
      bgpositionHM: '-55rpx -226rpx',
      bgpositionTW: '-55rpx -226rpx',
      bgpositionPassport: '-55rpx -226rpx',
      cardType: cardType,
      cardTypeFlag: true
    });
  }, pouponHM: function (e) {
    var cardType = e.currentTarget.dataset.cardtype;
    this.setData({
      bgpositionID: '-55rpx -226rpx',
      bgpositionHM: '0 -225rpx',
      bgpositionTW: '-55rpx -226rpx',
      bgpositionPassport: '-55rpx -226rpx',
      cardType: cardType,
      cardTypeFlag: true
    });
  },
  pouponTW: function (e) {
    var cardType = e.currentTarget.dataset.cardtype;
    this.setData({
      bgpositionID: '-55rpx -226rpx',
      bgpositionHM: '-55rpx -226rpx',
      bgpositionTW: '0 -225rpx',
      bgpositionPassport: '-55rpx -226rpx',
      cardType: cardType,
      cardTypeFlag: true
    });
  },
  pouponPassport: function (e) {
    var cardType = e.currentTarget.dataset.cardtype;
    this.setData({
      bgpositionID: '-55rpx -226rpx',
      bgpositionHM: '-55rpx -226rpx',
      bgpositionTW: '-55rpx -226rpx',
      bgpositionPassport: '0 -225rpx',
      cardType: cardType,
      cardTypeFlag:true
    });
  },
  tapName:function(){  
    var that = this;
    var pages = getCurrentPages();
    var prePage = pages[pages.length - 1];  
    if (!that.data.id){
      var name = that.data.name;
      var customerType = that.data.customerType;
      var cardNo = that.data.cardNo;
      var cardType = that.data.cardType;
      var telephone = that.data.telephone;
      if (!name){
        wx.showToast({
          title: '请填写乘客姓名！',
          icon: 'success',
          duration: 2500
        })
        return;
      }
      if (!customerType) {
        wx.showToast({
          title: '请选择乘客类型！',
          icon: 'success',
          duration: 2500
        })
        return;
      }
      if (!cardType) {
        wx.showToast({
          title: '请选择证件类型！',
          icon: 'success',
          duration: 2500
        })
        return;
      }
      if ('0' == cardType){
        if (!util.isCardNo(cardNo)) {
          wx.showToast({
            title: '请填写正确身份证号！',
            icon: 'success',
            duration: 2500
          })
          return;
        }
      }
      if (!util.checkPhone(telephone)) {
        wx.showToast({
          title: '请填写正确手机号！',
          icon: 'success',
          duration: 2500
        })
        return;
      }
      
      var params = { "opendId": app.globalData.opendId, "name": name, "customerType": customerType, "cardNo": cardNo, "cardType": cardType, "telephone": telephone };
      var url = apiHelp.addFrequentContact();


      apiHelp.request(url, 'POST', params, function (data, err) {
        wx.navigateBack({
          delta: prePage
        })
        /* wx.redirectTo({
           url: '../personlist/person_list'
         })*/
      });
      
    }else{
      var id = that.data.id;
      var name = that.data.name;
      var customerType = that.data.customerType;
      var cardNo = that.data.cardNo;
      var cardType = that.data.cardType;
      var telephone = that.data.telephone;
      if (!name) {
        wx.showToast({
          title: '请填写乘客姓名！',
          icon: 'success',
          duration: 2500
        })
        return;
      }
      if (!customerType) {
        wx.showToast({
          title: '请选择乘客类型！',
          icon: 'success',
          duration: 2500
        })
        return;
      }
      if (!cardType) {
        wx.showToast({
          title: '请选择证件类型！',
          icon: 'success',
          duration: 2500
        })
        return;
      }
      if ('0' == cardType) {
        if (!util.isCardNo(cardNo)) {
          wx.showToast({
            title: '请填写正确身份证号！',
            icon: 'success',
            duration: 2500
          })
          return;
        }
      }
      if (!util.checkPhone(telephone)) {
        wx.showToast({
          title: '请填写正确手机号！',
          icon: 'success',
          duration: 2500
        })
        return;
      }

      var params = { "id": id, "name": name, "customerType": that.data.customerType, "cardType": that.data.cardType, "cardNo": cardNo, "telephone": telephone };
      var url = apiHelp.updateFrequentContact();
      apiHelp.request(url, 'POST', params, function (data, err) {
        /*wx.redirectTo({
          url: '../personlist/person_list'
        })*/
        wx.navigateBack({
          delta: prePage
        })
      });
    }
   
  },
  getCustomerName:function(e){
    this.setData({
      name:e.detail.value
    })    
  },
  getCardNo:function(e){
    this.setData({
      cardNo: e.detail.value
    }) 
  },
  getTelephone:function(e){
    this.setData({
      telephone: e.detail.value
    }) 
  },
  delContact:function(){
    var url = apiHelp.delContact();
    var id = this.data.id;
    apiHelp.request(url, 'GET', {"id":id}, function (data, err) {
     wx.redirectTo({
         url: '../personlist/person_list'
       })
    });
  }
})